package com.queue;

import lombok.SneakyThrows;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


/**
 * @ProjectName: study
 * @Package: com.queue
 * @ClassName: ArrayBlockingQueue
 * @Author: zzh
 * @Description: 有界队列
 * @Date: 2021/11/26 10:40
 * @Version: 1.0
 */
public class ArrayBlockingQueueDemo {


    public static final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
            5,
            20,
            3000,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(100));

    @SneakyThrows
    public static void main(String[] args) {
      BlockingQueue queue =  new ArrayBlockingQueue(1024);
        for (int i = 0; i < 10; i++) {
            int finalI = i;
            threadPoolExecutor.submit(new Runnable() {
                @SneakyThrows
                @Override
                public void run() {
                    Thread.sleep(1000);
                    queue.put(finalI);
                }
            });
        }

        for (int i = 0; i < 10; i++) {
            System.out.println(queue.take());
        }

    }
}
